﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace MethodWorx.CMS.TemplateLanguage.Design
{
    public class DesignerControlBuilder
    {
        private List<string> preContent = new List<string>();
        private List<string> postContent = new List<string>();
        private List<string> childControls = new List<string>();
        private string name;

        public DesignerControlBuilder(string name)
        {
            this.name = name;
            this.ControlType = DesignerControlType.Container;
        }

        public DesignerControlBuilder AddContentBeforeChildren(string content)
        {
            preContent.Add(content);
            return this;
        }
    

        public DesignerControlBuilder AddContentAfterChildren(string content)
        {
            postContent.Add(content);
            return this;
        }

        public string Name { get { return this.name; } }
        public DesignerControlType ControlType { get; private set; }
        public string Width { get; private set; }

        public IEnumerable<string> ChildControls { get { return this.childControls.AsReadOnly();  } }

        public string GetContentPreChildren()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var s in preContent)
                sb.Append(s);

            sb.Append("<render:children />");

            return sb.ToString();
        }

        public DesignerControlBuilder SetControlType(DesignerControlType type)
        {
            this.ControlType = type;
            return this;
        }

        public DesignerControlBuilder AddChildControl(string element)
        {
            this.childControls.Add(element);
            return this;
        }

        public string GetContentPostChildren()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var s in postContent)
                sb.Append(s);

            return sb.ToString();
        }
    }
}
